#include "main.h"
#include <math.h>

sterilizing_runing::sterilizing_runing(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::sterilizing_runing)
{
    ui->setupUi(this);
    ui->pressure_display->setpressure_display();
     timer=new QTimer();
     QObject:: connect( timer, SIGNAL(timeout()), this,SLOT(timeDone()) );
     timer_working=1;
    connect( ui->countdown_timer, SIGNAL(overtime_signal()), this,SLOT(overtime_timeDone()) );
    connect( controller, SIGNAL(set_count_down_timer(uint)), SLOT(slot_recieve_timer_set(uint)) );
    connect( controller, SIGNAL(set_status_display(uint)), SLOT(slot_recieve_status_display(uint)));
    ui->exit_btn->setIcon(qApp->style()->standardIcon(QStyle::SP_ArrowBack));
    ui->logo->setPixmap(QPixmap(":/images/logo.png","png"));
    ui->logo->show();
}

sterilizing_runing::~sterilizing_runing()
{
   delete ui;
   delete timer;
}

void sterilizing_runing::slot_recieve_timer_set(uint n)
{
    timer_working=0;
    timer_count=n;
}


void sterilizing_runing::slot_recieve_status_display(uint n)
{
    if(n<100)
    {
    status_changed=1;
    sterilization_stage=n;
    }
    else
    {
     (stype->is_pulsing_vacuum)?
       ui->work_state_display->setText(trUtf8("脉动阶段")+QString::number(n-100)):
       ui->work_state_display->setText(trUtf8("排冷阶段")+QString::number(n-100));
    }
}

void sterilizing_runing::entrance()
{
    interface_handle();
    showhidden();
    showFullScreen();
    heating_valve_ON
    if(door_closed_OK && PSU>0) exhaust_valve_ON
    slot_recieve_status_display(0);//DISPLAY(0)
    timer->start(500);
    ui->countdown_timer->hide();
    workDone=0;
    recdnr=sterilization_time/60+1;
    if(sterilization_time%60) recdnr++;
    if(test_mode) ui->test_Button->show();
    else  ui->test_Button->hide();

}
void sterilizing_runing::start_running()
{
   controller->start();
   breakdown= NO_ERROR ;
   ui->continue_btn->setText(trUtf8("停止"));
   ui->continue_btn->SET_COLOR_RED
   hide_unused_when_running();
   if( sterilization_protype == DRY_PROTYPE) {  ui->runningtime_counter->counter_start(60000);}
}


inline void sterilizing_runing::stop_running()
{
     STOP_CMD
     LEDB_OFF
     showhidden();
     ui->work_state_display->setText(trUtf8("灭菌停止"));
     // temperature_keep=0;
   // heating_valve_OFF;
}


 void sterilizing_runing::interface_handle()
{

     ui->title->setText(str_sterilization_type);
     ui->sterilization_temperature_label->setNum(sterilization_temperature);
     ui->sterilization_time_label->setNum(sterilization_time);
     ui->print_btn->hide();
     ui->F0_label->hide();
     ui->F0_value->hide();

     if(stype->is_pulsing_vacuum)
     {
         ui->pulsing_times_label->setText(trUtf8("脉动次数"));
         ui->pulsing_times_display->setNum(pulsing_times);
         ui->dry_times_label->setText(trUtf8("干燥次数"));
         ui->dry_times_display->setNum(dry_times);
         ui->dry_times_label->show();
         ui->dry_times_display->show();
     }
     else
     {
         ui->pulsing_times_label->setText(trUtf8("排冷次数"));
         ui->pulsing_times_display->setNum(cold_exhaust_times);
         if(sterilization_protype == LIQUID_RPOTYPE)
              {

                  ui->dry_times_label->setText(trUtf8("参考F0值"));;
                  ui->dry_times_display->setNum( (int)reference_f0);
                  ui->dry_times_label->show();
                  ui->dry_times_display->show();
                  ui->F0_label->show();
                  ui->F0_value->show();
              }
              else
             {
                 ui->dry_times_label->hide();
                 ui->dry_times_display->hide();
            }

     }

     if(continuous_mode) ui->label_title->setText(trUtf8("灭菌程序正在运行(连续模式)"));
     else  ui->label_title->setText(trUtf8("灭菌程序正在运行"));

     if( whole_machine_parameter.bottom_exhaust_machine)
     {
          ui->air_back_btn->hide();
     }
     else
     {
          ui->air_back_btn->show();
     }

}


 inline void sterilizing_runing::hide_unused_when_running()
 {
     ui->print_btn->hide();
     ui->exit_btn->hide();
     ui->door_operation_btn->hide();
     ui->air_back_btn->hide();
     ui->exhaust_btn->hide();

 }

 inline void sterilizing_runing::showhidden()
  {
    ui->continue_btn->SET_COLOR_GREEN
    ui->continue_btn->setText(trUtf8("启动"));
    ui->door_operation_btn->show();
    ui->print_btn->show();
    if(!whole_machine_parameter.bottom_exhaust_machine)    ui->air_back_btn->show();
     ui->exhaust_btn->show();
     ui->exit_btn->show();
     ui->runningtime_counter->stop();
     ui->countdown_timer->stop();//clear();
     ui->countdown_timer->close_overtimer();

  }


void sterilizing_runing::on_exit_btn_clicked()
{
    BUTTON_SOUND;

    if( Affirm("确认是否退出灭菌程序？"))
  {
   //w->showFullScreen();
  // stop_running();
   STOP_CMD
   LEDB_OFF
   timer->stop();
   exhaust_valve_OFF
   air_supply_valve_OFF
   close();
    if(sterilization_protype == DRY_PROTYPE)
   {
       ui->dry_times_label->show();
       ui->dry_times_display->show();
       ui->label_2->show();
       ui->label_4->show();
       ui->label_5->show();
    //   ui->label_6->show();
       ui->label_7->show();
       ui->sterilization_temperature_label->show();
       ui->sterilization_time_label->show();
       ui->pulsing_times_label->show();
       ui->pulsing_times_display->show();
    //   ui->work_state_display->show();
   }
  }
}

void sterilizing_runing::on_exhaust_btn_clicked()
{
    BUTTON_SOUND;
 //   if( !sterilizing)
  {
    if(ui->exhaust_btn->isChecked() )
  {
     ui->exhaust_btn->SET_COLOR_GREEN
     exhaust_valve_ON
  }
    else{
        ui->exhaust_btn->SET_COLOR_GRAY
        exhaust_valve_OFF
    }
    }
}


void sterilizing_runing::on_continue_btn_clicked()
{
    BUTTON_SOUND;

   if(sterilizing)
 { 
       if(Affirm("确认是否停止灭菌？"))
    {
      breakdown=MANUAL_STOP;
      stop_running();
    }   
  }
   else
   {
         if(workable) start_running();
         else if(!door_closed_OK){ WARN->show_text("请把门关好后继续！");}
         else if(water < normal)   WARN->show_text("缺水！");
   }

}

void sterilizing_runing::on_print_btn_clicked()
{
    BUTTON_SOUND;
    if( Affirm("确认是否打印？"))
    {  serialprinter->print_file(); }

}



 void sterilizing_runing::on_air_back_btn_clicked()
 {
      BUTTON_SOUND;
    //  if( !sterilizing)
    {
       if(ui->air_back_btn->isChecked())
      {
      ui->air_back_btn->SET_COLOR_GREEN
      air_supply_valve_ON;
       }
       else
       {
       air_supply_valve_OFF;
       ui->air_back_btn->SET_COLOR_GRAY
       }
    }

 }

void sterilizing_runing::timeDone()
{
     qApp->processEvents();

     door();
     action_display();
     stage_display();

     if(!timer_working)
   {
     ui->countdown_timer->countdown(timer_count);
     timer_working=1;
   }
    if( inter_pressure_switch_isreached && sterilization_stage==0 )
     {
      ui->work_state_display->setText(trUtf8("预热完成"));
     }

         if( ( (!sterilizing && !continuous_mode) || !workable) && ui->door_operation_btn->isHidden() )
        {
             showhidden();
         }

         if(sterilization_protype==LIQUID_RPOTYPE)  show_F0();

         if(air_supply_valve_isopen)
      {
             if( !ui->air_back_btn->isChecked())
         {     
         ui->air_back_btn->setChecked(true);
         ui->air_back_btn->SET_COLOR_GREEN
          }
       }
         else
       {
            if( ui->air_back_btn->isChecked())
            {
             ui->air_back_btn->setChecked(false);
             ui->air_back_btn->SET_COLOR_GRAY
            }
        }

         if(exhaust_valve_isopen)
      {
             if( !ui->exhaust_btn->isChecked())
         {
             ui->exhaust_btn->setChecked(true);
             ui->exhaust_btn->SET_COLOR_GREEN
          }
       }
         else
       {
         if( ui->exhaust_btn->isChecked())
         {
          ui->exhaust_btn->setChecked(false);
          ui->exhaust_btn->SET_COLOR_GRAY
         }
       }

      //if(sterilizing && sterilization_stage==2 && water< normal)
        if(!sterilizing)    ui->countdown_timer->close_overtimer();

        static int count;
        if(water_valve_isopen)
        {
         // ui->water_btn->setChecked(true);
         // ui->water_btn->SET_COLOR_GREEN
          ui->water_level->setValue(count++);
          if(count==7) count=0;
        }
        else  {
             if(water) ui->water_level->setValue( ( water - leak-1)*3);
             else ui->water_level->setValue( 0);
          //   ui->water_btn->SET_COLOR_GRAY
          //   ui->water_btn->setChecked(false);
                }

      qApp->processEvents();
}

void sterilizing_runing::show_F0()
{
    ui->F0_value->setText(QString::number(controller->F0,'f',1));

}


void sterilizing_runing::on_door_operation_btn_clicked()
{
    BUTTON_SOUND;
    door_operation->enter();
}


inline void sterilizing_runing::door()
{
    if(workDone && !door_closed_OK) {
    ui->work_state_display->setText(trUtf8("请继续或退出"));
    workDone=0;
    LEDB_OFF
    }

    if(ui->door_operation_btn->isVisible())
    {
  if(door_closed_OK) {
      ui->door_operation_btn->SET_COLOR_GREEN
      ui->door_operation_btn->setText(trUtf8("门已锁"));}
  else {
      ui->door_operation_btn->SET_COLOR_RED
      ui->door_operation_btn->setText(trUtf8("门未锁"));}
    }

}

inline void sterilizing_runing::action_display()
{

    if(Vacuum_pump_isopen)
   {
   ui->Vacuum_pump_display->setText(trUtf8("正在抽气"));
   ui->Vacuum_pump_display->SET_COLOR_GREEN
   ui->Vacuum_pump_display->show();
    }
    else
    {
    ui->Vacuum_pump_display->hide();
    }

    if(air_intake_valve_isopen)
   {
   ui->air_intake_display->setText(trUtf8("正在进气"));
   ui->air_intake_display->SET_COLOR_GREEN
   ui->air_intake_display->show();
    }
    else
    {
    ui->air_intake_display->hide();
    }

    if(exhaust_valve_isopen)
   {
   ui->Exhaust_display->setText(trUtf8("正在排气"));
   ui->Exhaust_display->SET_COLOR_GREEN
   ui->Exhaust_display->show();
    }
    else
    {
      ui->Exhaust_display->hide();
    }

    if(air_supply_valve_isopen)
   {
   ui->air_supply_display->setText(trUtf8("正在回气"));
   ui->air_supply_display->SET_COLOR_GREEN
   ui->air_supply_display->show();
    }
    else
    {
      ui->air_supply_display->hide();
    }

    if(heating_valve_isopen)
   {
   ui->heating_display->setText(trUtf8("正在加热"));
   ui->heating_display->SET_COLOR_GREEN
   ui->heating_display->show();
    }
    else
    {
      ui->heating_display->hide();
    }

}

inline void sterilizing_runing::stage_display()
 {
    if(status_changed)
   {
       switch(sterilization_stage)
       {
       case 0:
           ui->work_state_display->setText(trUtf8("正在预热"));
         //  temperature_keep=0;
           // exhaust_valve_ON
           workDone=0;
           break;
       case 1:
               controller->rec->starttime=QDateTime::currentDateTime().toString("yyyyMMdd-hhmmss");
             (stype->is_pulsing_vacuum)?
                ui->work_state_display->setText(trUtf8("脉动阶段")):
                ui->work_state_display->setText(trUtf8("排冷阶段"));
                ui->runningtime_counter->counter_start(60000);
                LEDA_ON
                LEDB_OFF
                controller->rec->record();
            break;
       case 2:
           ui->countdown_timer->open_overtimer( prebalance_time+900);
           ui->work_state_display->setText(trUtf8("升温阶段"));
           controller->rec->auto_record();
           controller->rec->stage_cut(2);
           STOP_AUTO_HEAT
           break;

       case 3:
           ui->countdown_timer->close_overtimer();
           ui->work_state_display->setText(trUtf8("正在灭菌"));
          // rec->stop_auto_record();
           controller->rec->auto_record();
           controller->rec->stage_cut(3);
           controller->rec->record();
           //RECORD_USED_TIMES
           whole_machine_parameter.used_times_counter++;
           config_changed=true;
           save_config(DATA_FILE);
           break;
       case 4:
            controller->rec->stop_auto_record();
           controller->rec->stage_cut(4);
           (stype->is_pulsing_vacuum)?
           ui->work_state_display->setText(trUtf8("正在干燥")):
           ui->work_state_display->setText(trUtf8("正在排气"));
          if( continuous_mode)  START_AUTO_HEAT
          else   heating_valve_OFF
           break;
       case 5:
           ui->work_state_display->setText(trUtf8("灭菌完成"));
           controller->rec->stage_cut(5);
           controller->rec->save();
           workDone=1;
           LEDB_ON
           LEDA_OFF
           if(continuous_mode) { start_running();  serialprinter->print_file();}// ui->continue_btn->click();
           else {showhidden(); }
            WARN->sound(20);
           break;
       case 10:ui->work_state_display->setText(trUtf8("灭菌失败！"));
           showhidden();
           STOP_ACTION;
           break;
       }
        status_changed=0;
   }


   }


void sterilizing_runing::overtime_timeDone()
{
    static uint overtimes;

    if(sterilization_stage==2)
    {
        if(++overtimes==2)
        {
        breakdown=OVERTIME;
        STOP_CMD
        overtimes=0;
         }
         else
        {
         ui->countdown_timer->open_overtimer(300);
        }
         WARN->show_sound("升温过程超时,检查手动排气阀是否关闭或联系维护人员！");

    }
}

void sterilizing_runing::on_sensor_adjust_Button_clicked()
{
    BUTTON_SOUND;
    if(input->password_check(supper_password,false))
    {
    MCD_Interface->set_datamap(&sensor_adjust_datamap,3);
    MCD_Interface->showFullScreen();

    }

}

 void sterilizing_runing::on_test_Button_clicked()
 {
    debug_dialog.show();

 }
